package com.qiang.work.test01;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class Demo02 {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo02.class);

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(12345);
        /*Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = df.format(date);
        InetAddress inetAddress = ss.getInetAddress();
        String hostAddress = inetAddress.getHostAddress();*/
        //LOGGER.warn(format + hostAddress+"发送成功");


        while (true) {
            Socket accept = ss.accept();

            //网络中的流,从客户端读取数据的
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            //本地的IO流,把数据写到本地中,实现永久化存储
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\dd\\" + UUID.randomUUID().toString() + ".jpg"));

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

            bos.close();
            accept.close();
            //ss.close();
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format = df.format(date);
            InetAddress inetAddress = accept.getInetAddress();
            String hostAddress = inetAddress.getHostAddress();
            LOGGER.warn(format);
            LOGGER.warn(hostAddress);
            LOGGER.warn("发送成功");
        }
    }
}
